package com.administrator.scoreManageSys.controller.course;

import com.administrator.scoreManageSys.service.CourseService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;

/**
 * 课程相关控制器
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/course/manage")
public class CourseController {

    @Resource
    private CourseService courseService;


    /**
     * 获取考季列表
     *
     * @return 列表
     */
    @PostMapping(value = "/seasonList")
    public JSONObject seasonList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            String name = data.getString("name");
            return courseService.seasonList(pageNum, pageSize,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取考季列表 异常");
        }
    }

    /**
     * 添加考季
     *
     * @return 列表
     */
    @PostMapping(value = "/seasonAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "season:add", logical = Logical.OR)
    public JSONObject seasonAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            //新添考季名称
            String name = data.getString("name");
            return courseService.seasonAdd(userId, name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加考季 异常");
        }
    }

    /**
     * 删除考季
     *
     * @return 列表
     */
    @PostMapping(value = "/seasonDelete")
    @RequiresRoles("education")
    @RequiresPermissions(value = "season:add", logical = Logical.OR)
    public JSONObject seasonDelete(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return courseService.seasonDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除考季 异常");
        }
    }

    /**
     * 修改考季
     *
     * @return 列表
     */
    @PostMapping(value = "/seasonUpdate")
    @RequiresRoles("education")
    @RequiresPermissions(value = "season:add", logical = Logical.OR)
    public JSONObject seasonUpdate(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            String name = data.getString("name");
            return courseService.seasonUpdate(id, name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除考季 异常");
        }
    }


    /**
     * 获取科目列表
     *
     * @return 列表
     */
    @PostMapping(value = "/subjectList")
    public JSONObject subjectList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            String name = data.getString("name");
            return courseService.subjectList(pageNum,pageSize,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取科目列表 异常");
        }
    }

    /**
     * 添加科目
     *
     * @return 列表
     */
    @PostMapping(value = "/subjectAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "subject:add", logical = Logical.OR)
    public JSONObject subjectAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            //新添科目名称
            String name = data.getString("name");
            return courseService.subjectAdd(userId, name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加科目 异常");
        }
    }

    /**
     * 删除科目
     *
     * @return 列表
     */
    @PostMapping(value = "/subjectDelete")
    @RequiresRoles("education")
    @RequiresPermissions(value = "subject:add", logical = Logical.OR)
    public JSONObject subjectDelete(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return courseService.subjectDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除科目 异常");
        }
    }

    /**
     * 修改科目
     *
     * @return 列表
     */
    @PostMapping(value = "/subjectUpdate")
    @RequiresRoles("education")
    @RequiresPermissions(value = "subject:add", logical = Logical.OR)
    public JSONObject subjectUpdate(@RequestBody JSONObject data) {
        try {
            //需要修改的科目id
            Long id = data.getLong("id");
            //修改科目名称
            String name = data.getString("name");

            return courseService.subjectUpdate(name, id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改科目 异常");
        }
    }

    /**
     * 获取课程考试列表
     *
     * @return 列表
     */
    @PostMapping(value = "/courseList")
    public JSONObject courseList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Integer subject = data.getInteger("subject");
            String name = data.getString("name");
            return courseService.courseList(pageNum,pageSize,subject,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取课程列表 异常");
        }
    }

    /**
     * 添加课程考试
     *
     * @return 列表
     */
    @PostMapping(value = "/courseAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "course:add", logical = Logical.OR)
    public JSONObject courseAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            //新添课程名称
            String name = data.getString("name");
            //添加的科目管理考季id
            Long subject = data.getLong("subject");
            return courseService.courseAdd(userId, name, subject);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加课程 异常");
        }
    }

    /**
     * 删除课程考试
     *
     * @return 列表
     */
    @PostMapping(value = "/courseDeletion")
    @RequiresRoles("education")
    @RequiresPermissions(value = "course:add", logical = Logical.OR)
    public JSONObject courseDeletion(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return courseService.courseDeletion(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除课程 异常");
        }
    }

    /**
     * 修改课程考试
     *
     * @return 列表
     */
    @PostMapping(value = "/courseUpdate")
    @RequiresRoles("education")
    @RequiresPermissions(value = "course:add", logical = Logical.OR)
    public JSONObject courseUpdate(@RequestBody JSONObject data) {
        try {
            //需要修改的科目id
            Long id = data.getLong("id");
            //修改课程名称
            String name = data.getString("name");
            //修改的科目id
            Long subject = data.getLong("subject");
            return courseService.courseUpdate(name, subject, id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改科目 异常");
        }
    }



}
